<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;
use App\Models\Loanapply;
use Illuminate\Http\Request;

class LoanapplyController extends AdminController {

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'loanapply';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '小一农货-贷款申请管理';
    }

    public function index(Request $request){
        $page = $request->get('page', 1);
        $search = $request->all();
        $this->_response['_active']['_action']      = 'list';

        $loanapplyModel = new Loanapply();

        $data = $loanapplyModel->getData($search, $page, 20);

        $this->_response['loanapply'] = $data;

        $this->_response['loanapply']['pageData']['page'] = $page;
        $this->_response['loanapply']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['loanapply']['pageData']['lastPage'] , '/admin/loanapply?');

        return view('admin.loanapply.list', $this->_response);
    }

    public function add(){
        $this->_response['_active']['_action']      = 'add';
        return view('admin.loanapply.add', $this->_response);
    }

    public function doAdd(\App\Http\Requests\Loanapply $request){
        $data = $request->all();
        $saveData = [
            'name' => $data['name'],
            'money' => $data['money'],
            'weixin' => $data['weixin'],
            'ishavehouse' => $data['ishavehouse'],
            'apply_time' => date('Y-m-d H:i:s', time()),
        ];

        $loanapplyModel = new Loanapply();
        if($loanapplyModel->saveData($saveData)){
            return response()->json(['code' => 0, 'msg' => '添加成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }

//    public function edit($id){
//        $this->_response['loanapply'] = Loanapply::where('id', $id)->first();
//
//        return view('admin.loanapply.edit', $this->_response);
//    }

    public function doedit(Request $request){
        $data = $request->all();
        $setData =[
            'check_status'=>1,
            'update_at' => date('Y-m-d H:i:s', time())
        ];
        $loanapplyModel = new Loanapply();
        if($loanapplyModel->setData($data['id'],$setData)){
            return response()->json(['code' => 0, 'msg' => '修改成功']);
            //return redirect('/admin/loanapply');
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }


    public function delete($id){
        $loanapplyModel = new Loanapply();
        $data=[
            'deleted_at'=>date('Y-m-d H:i:s',time())
        ];
        if($loanapplyModel->delData($id,$data)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
            return redirect('/admin/loanapply');
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }

}